//
//  ChatBarMoreView.m
//  keyboard
//
//  Created by 遇水 on 16/3/12.
//  Copyright © 2016年 遇水. All rights reserved.
//

#import "ChatBarMoreView.h"
#import "UIView+Extension.h"
#import "ChatBarMoreViewItem.h"

@interface ChatBarMoreView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView    *scrollView;

@property (nonatomic, strong) UIPageControl    *pageControl;

@property (nonatomic, strong) NSArray<ChatBarMoreViewItemModel *>    *models;

//@property (nonatomic, strong) NSMutableArray    *cellArray;     //用来存放所有的item

@end


@implementation ChatBarMoreView


-(instancetype)init
{
    self = [super init];
    if (self) {
        _numberForLine = 4;
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubview];
    }
    return  self;
}

/**
 *  初始化子控件
 */
-(void)initSubview
{
    //添加scrollView
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, (self.height - 20))];
    //20 是下边pageControl 的高度
    scrollView.showsVerticalScrollIndicator  =NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    
    [self addSubview:scrollView];
    self.scrollView = scrollView;
    
    
    //添加pageControl
    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.scrollView.height, self.width, 20)];
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor darkGrayColor];
    pageControl.hidesForSinglePage = YES;
    
    [self addSubview:pageControl];
    self.pageControl = pageControl;

}

- (void)setDataSource:(id<ChatBarMoreViewDatasource>)dataSource
{
    _dataSource =dataSource;
    
    [self reloadData];
}

-(void)setNumberForLine:(NSUInteger)numberForLine
{
    _numberForLine = numberForLine;
    
    [self reloadData];
}


-(void)reloadData
{
    CGFloat edgeW = 5;
    CGFloat cellW = (self.scrollView.width - 5 * edgeW) / 4;
    CGFloat cellH = (self.scrollView.height - 3* edgeW) / 2;
    
    self.models = [self.dataSource moreViewOfDataSource:self];
    //将现有的清除掉
    //重写设置cell,每行4个，两行
     NSUInteger lineNum = 0;         //行数
     NSUInteger columnNum = 0 ;      //列数
     NSUInteger pageNum = 0;         //页数
    
    
    for (ChatBarMoreViewItemModel *model in self.models) {
        //遍历内容，设置内容
        if (columnNum >=4) {
            lineNum ++ ;
            columnNum = 0;
        }
        if (lineNum > 1) {
            lineNum = 0;
            columnNum = 0;
            pageNum++;
        }
        
        CGFloat X = edgeW + columnNum *(cellW + edgeW) + pageNum * self.width;
        CGFloat Y = edgeW +  lineNum * (cellH + edgeW);
         
        ChatBarMoreViewItem *cell = [[ChatBarMoreViewItem alloc]initWithFrame:CGRectMake(X, Y, cellW, cellH)];
        [cell setViewWithTitle:model.title imageName:model.imageName tag:model.type];
        
        [cell addTarget:self action:@selector(cellAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:cell];
        columnNum++;            //下移列

    }
    
    self.scrollView.contentSize = CGSizeMake(self.scrollView.width *(pageNum +1), 0);

}

-(void)cellAction:(ChatBarMoreViewItem *)cell
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(chatBarMoreView:didSelectedAtIndex:)])
    {
        [self.delegate chatBarMoreView:self didSelectedAtIndex:cell.tag];
    }
}


//更改pageControl
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double pageNum = scrollView.contentOffset.x / scrollView.width;
    self.pageControl.currentPage  =(int)(pageNum + 0.5);
}


@end
